#include"stack.h"

void StackInit(st* st)
{
	assert(st);
	st->arr = NULL;
	st->capacity = st->top = 0;
}

void StackDestroy(st* st)
{
	assert(st);
	if(st->arr!=NULL)
	free(st->arr);

	st->arr = NULL;
	st->capacity = st->top = 0;
}

void StackPush(st* st, elemtype x)
{
	assert(st);
	if (st->top >= st->capacity)
	{
		int newcapacity = st->capacity == 0 ? 4 : 2 * st->capacity;
		elemtype *tmp = (elemtype*)realloc(st->arr, newcapacity * sizeof(elemtype));
		if (NULL == tmp)
		{
			perror("realloc");
			return -1;
		}
		st->arr = tmp;//
		st->capacity = newcapacity;//
	}
	st->arr[st->top++] = x;
}


bool StackEmpty(st*st)
{
	assert(st);
	return (st->top == 0);
}

void StackPop(st* st)
{
	assert(!StackEmpty(&st));
	--st->top;
}

elemtype Stacktop(st* st)
{
	assert(!StackEmpty(&st));
	return st->arr[st->top-1];
}





